﻿using DynamicViewFormApp.Component;
using DynamicViewFormApp.Entity;
using DynamicViewFormApp.Enum;
using DynamicViewFormApp.Handler;
using DynamicViewFormApp.Setting;
using DynamicViewFormApp.Util;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DynamicViewFormApp.Service
{
    class PageService
    {
        private static readonly int MARGIN_LEFT = 132;
        private static PageEntity pageEntity;
        //private static int subfield = 2;//分栏数

        /// <summary>
        /// 根据父控件信息，计算控件调整位置目标坐标
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        internal static void ResizeLocation(Panel p)
        {
            foreach (Control ctl in p.Controls)
            {
                int x = (int)(0.5 * (p.Width + MARGIN_LEFT - ctl.Width));
                x = Math.Abs(x);
                int y = ctl.Location.Y;
                ctl.Location = new Point(x, y);
            }
        }

        /// <summary>
        /// 新增一个页面
        /// </summary>
        /// <param name="panelBase"></param>
        /// <param name="pageSizeType"></param>
        /// <returns></returns>
        internal static Panel CreateNewPage(Panel panelBase)
        {
            int width = 0;
            int height = 0;
            int pageOffSet = PageSetting.PageOffSet;
            int subfield = PaperSetting.Subfield;//分栏数
            switch (PaperSetting.Type)
            {
                case "A3":
                    width = A3.Width;
                    height = A3.Height;
                    subfield = 2;
                    break;
                case "A4":
                    width = A4.Width;
                    height = A4.Height;
                    subfield = 1;
                    break;
            }
            //更新静态变量
            PaperSetting.Width = width;
            PaperSetting.Height = height;
            PaperSetting.Subfield = subfield;

            Point location = PageService.GetLocation(panelBase, width);//获取页面坐标
            Size pageSize = new Size(width, height);

            string id = Guid.NewGuid().ToString("N");
            PaperSetting.CurrentPageId = id;
            Panel page = new Panel
            {
                Name = id,
                Size = pageSize,
                Location = location,
                BackColor = Color.White
            };
            // 绘制控件背景分栏边框
            pageEntity = DrawUtil.DrawSolidRectangle(page, pageOffSet, subfield);
            panelBase.Controls.Add(page);


            return page;
        }


        /// <summary>
        /// 根据父控件信息，计算控件目标坐标
        /// </summary>
        /// <param name="p"></param>
        /// <param name="w"></param>
        /// <returns></returns>
        internal static Point GetLocation(Panel p, int w)
        {
            int X = A4.Span, Y = A4.Span;
            foreach (Control ctl in p.Controls)
            {
                int x = (int)(0.5 * (p.Width + MARGIN_LEFT - ctl.Width));
                int y = ctl.Location.Y;
                ctl.Location = new Point(x, y);
                Y = ctl.Location.Y + ctl.Height + A4.Span;
            }

            X = (int)(0.5 * (p.Width + MARGIN_LEFT - w));
            //调整位置
            return new Point(X, Y);
        }

        /// <summary>
        /// 创建基础文本框
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        internal static RichTextBox CreateRichTextBox(Control page)
        {
            int pageOffSet = PageSetting.PageOffSet;
            string id = Guid.NewGuid().ToString("N");

            RichTextBox rtb = new RichTextBox();

            //rtb.MouseUp += new MouseEventHandler(PageHander.Component_MouseUp);
            //rtb.MouseDown += new MouseEventHandler(PageHander.Component_MouseDown);
            //rtb.MouseMove += new MouseEventHandler(PageHander.Component_MouseMove);
            //rtb.GotFocus += new EventHandler(RichTextBoxHandler.GotFocus);
            //rtb.LostFocus += new EventHandler(RichTextBoxHandler.LostFocus);
            //rtb.MouseHover += new EventHandler(RichTextBoxHandler.MouseHover);
            //rtb.MouseLeave += new EventHandler(RichTextBoxHandler.MouseLeave);

            rtb.Location = new Point(pageOffSet + pageOffSet / 2, pageOffSet + pageOffSet / 2);

            rtb.Width = pageEntity.Width - pageOffSet;
            rtb.Height = pageOffSet;
            rtb.BackColor = Color.White;

            Font font = FontSetting.FontContent;
            rtb.Font = font;

            rtb.BorderStyle = BorderStyle.None;

            page.Controls.Add(rtb);
            return rtb;
        }

    }
}
